import random
import pygame
#定义坐标类型
class Point:
    def __init__(self, row, col):
        self.row = row
        self.col = col

    def follow(self):
        return Point(self.row, self.col)

# 初始化
pygame.init()
#初始化变量
speed = 20
wide = 800
high = 600
Row = 30
Col = 40
size = (wide, high)
window = pygame.display.set_mode(size)
score = 0
pygame.display.set_caption("贪吃蛇")
black = (0, 0, 0)
white = (255, 255, 255)

# 定义头和食物坐标
head = Point(row=Row / 2, col=Col / 2)
head_color = (0, 128, 128)
food = Point(row=random.randint(2, Row - 2), col=random.randint(2, Col - 2))
food_color = (255, 255, 0)

# 定义移动列表
snake_color = (128, 128, 128)
snake = [
    Point(row=head.row, col=head.col + wide / Col),
    Point(row=head.row, col=head.col + wide / Col * 2),
    Point(row=head.row, col=head.col + wide / Col * 3)
]

#初始方向
direct = "left"

# 绘制函数
def rect(Point, color):
    cell_w = wide / Col
    cell_h = high / Row
    left = Point.col * cell_w
    top = Point.row *  cell_h
    pygame.draw.rect(window, color, (left, top, cell_w, cell_h))


# 游戏循环
running = True
while running:
    # 帧率和运动速度
    clock = pygame.time.Clock()
    clock.tick(120)
    t = clock.tick(120) / 1200
    for event in pygame.event.get():

        # 退出游戏
        if event.type == pygame.QUIT:
            running = False

            # 移动按钮
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and direct != "down":
                direct = "up"
            elif event.key == pygame.K_DOWN and direct != "up":
                direct = "down"
            elif event.key == pygame.K_LEFT and direct != "right":
                direct = "left"
            elif event.key == pygame.K_RIGHT and direct != "left":
                direct = "right"
    font = pygame.font.Font(None, 36)  # 使用默认字体，字号为36
    text = font.render(f"Score: {score}", True, black)  # 生成文本图像
    window.blit(text, (10, 10))  # 在屏幕上的(10, 10)位置绘制文本

    # 吃东西
    eat = (food.row - 1 < head.row < food.row + 1 and food.col - 1 < head.col < food.col + 1)

    # 身子跟随
    snake.insert(0, head.follow())
    if eat:
        score += 1
        food = Point(row=random.randint(2, Row - 2), col=random.randint(2, Col - 2))
    if not eat:
        snake.pop()

    # 移动
    if direct == "left":
        head.col -= speed*2*t
    elif direct == "right":
        head.col -= -speed*2*t
    elif direct == "up":
        head.row += -speed*2*t
    elif direct == "down":
        head.row += speed*2*t

    #检测碰撞
    dead = False
    if head.col <=0 or head.row <=0 or head.col >= Col or head.row >= Row:
        dead = True
    for body in snake:
        if body.col+10<head.col < body.col+10 and body.row-10 <head.row < body.row+10:
            dead = True
            break
    if dead:

        # 结算画面
        window.fill(black)  # 清屏
        font = pygame.font.Font(None, 66)  # 使用默认字体，较大的字号用于结算画面
        text = font.render(f"Game Over    Score: {score}", True, white)
        window.blit(text, (wide // 2 - text.get_width() // 2, high // 2 - text.get_height() // 2))  # 居中显示
        pygame.display.flip()
        pygame.time.wait(3000)
        running=False
        pygame.quit()

    # 渲染
    pygame.display.flip()

    # 背景
    pygame.draw.rect(window, (255, 255, 255), (0, 0, wide, high))

    #绘制蛇和食物
    for body in snake:
        rect(body, snake_color)

    rect(head, head_color)
    rect(food, food_color)